home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / NeoIntroTCL3.0 folder / TCL / Laughs / Source / CLaughsDoc.cp < prev    next >
Encoding:
Text File  |  1994-10-02  |  5.2 KB  |  228 lines  |  [TEXT/KAHL]

  1. /****
  2.  * CLaughsDoc.cp
  3.  *
  4.  *    Implementation of a document class full of laughs.
  5.  *
  6.  *  Copyright © 1994 NeoLogic Systems.  All rights reserved.
  7.  *
  8.  *    This sample app is derived from a contribution originally made
  9.  *    by Paul Gee (Gee, thanks Paul!).
  10.  *
  11.  ****/
  12.  
  13. #include "NeoTypes.h"
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <time.h>
  17. #include CNeoDatabaseNativeH
  18. #include CNeoIndexIteratorH
  19. #include CNeoSelectH
  20. #include "CLaughsApp.h"
  21. #include "CLaughsDoc.h"
  22. #include "CPerson.h"
  23.  
  24. /***
  25.  * CLaughsDoc
  26.  *
  27.  *    This is the document's initialization method.
  28.  *
  29.  ***/
  30. CLaughsDoc::CLaughsDoc(CApplication *aSupervisor, const Boolean aPrintable, const Boolean aNewDatabase)
  31.     : CNeoDocRoot(kLaughsSig, kLaughsFileType, aPrintable, aNewDatabase, TRUE, FALSE)
  32. {
  33.     // A document that opens a window would not need to activate itself explicitly.
  34.     Activate();
  35. }
  36.  
  37. /***
  38.  * buildWindow
  39.  *
  40.  *    This is the auxiliary window-building method that the
  41.  *    NewFile and OpenFile methods use to create a window.
  42.  *
  43.  *    Laughs doesn't need to explicitly create a window,
  44.  *    printf will do that for us.
  45.  *
  46.  ***/
  47. void CLaughsDoc::buildWindow(void)
  48. {
  49.     // Nothing to do.
  50. }
  51.  
  52. /***
  53.  * NewFile
  54.  *
  55.  *    When the user chooses New from the File menu, the createDocument
  56.  *    method in your application will call the newly created document's
  57.  *    NewFile message.
  58.  *
  59.  *    This method should begin by calling inherited. After that it might
  60.  *    want to title the window and set the default name which will appear
  61.  *    in the standard get file dialog. Laughs calls DoSaveFileAs just
  62.  *    so the user doesn't have to choose the Save menu item. Your
  63.  *    application may decide otherwise.
  64.  *
  65.  ***/
  66. void CLaughsDoc::NewFile(void)
  67. {
  68.     CNeoDatabase *    database        = getDatabase();
  69.  
  70.     inherited::NewFile();
  71.  
  72.     // Set the default name which appears in the get file dialog.
  73.     database->Specify("\pLaughter", 0);
  74.  
  75.     // Stuff the database with objects.
  76.     createObjects();
  77.  
  78.     // Commit the changes that we made to the database.
  79.     DoSaveFileAs();
  80.  
  81.     printf("\nDone!\n\n");
  82. }
  83.  
  84. /***
  85.  * OpenFile
  86.  *
  87.  *    When the user chooses Open… from the File menu, the OpenDocument
  88.  *    method in your application class will let the user choose a file
  89.  *    and then send a newly created document this message. The information
  90.  *    about the file is in the SFReply record.
  91.  *
  92.  ***/
  93. void CLaughsDoc::OpenFile(SFReply *macSFReply)
  94. {
  95.     fOpenMode = fsRdPerm;
  96.     inherited::OpenFile(macSFReply);
  97.  
  98.     // Find each of the objects in the database in turn.
  99.     printOut();
  100.  
  101.     // That's all folks!
  102.     printf("\nDone!\n\n");
  103. }
  104.  
  105. /***
  106.  * createObjects
  107.  *
  108.  *    Add three people to the database, two jokers and a clown.
  109.  *
  110.  ***/
  111. void CLaughsDoc::createObjects(void)
  112. {
  113.     CJoke *            joke1;
  114.     CJoke *            joke2;
  115.     CJoker *        joker;
  116.     CClown *        clown;
  117.     CNeoDatabase *    database        = getDatabase();
  118.     CNeoString        string;
  119.     char            name[64];
  120.  
  121.     // Tell them what we're about to do.
  122.     database->getName(string);
  123.     BlockMove(&string[1], name, string[0]);
  124.     name[string[0]] = 0;
  125.     printf("Storing 2 Jokers & a Clown in \"%s\".\n", name);
  126.  
  127.     // Know any good jokes? How 'bout this one...
  128.     joke1 = new CJoke("The world’s shortest poem: Flees. Adam had'em.");
  129.  
  130.     // Add it to the database.
  131.     // Note: An object ID is assigned automaticly by addObject.
  132.     database->addObject(joke1);
  133.  
  134.     // Is this a joke???
  135.     joke2 = new CJoke("My dogs got no nose?");
  136.  
  137.     // Add it to the database.
  138.     database->addObject(joke2);
  139.  
  140.     // Create a joker object.
  141.     joker = new CJoker("\pJack");
  142.  
  143.     // Teach it a couple of jokes.
  144.     joker->learnJoke(joke1);
  145.     joker->learnJoke(joke2);
  146.  
  147.     // Add it to the database.
  148.     database->addObject(joker);
  149.  
  150.     // Don't need this guy any more. Remove our reference to it.
  151.     joker->unrefer();
  152.     joker = nil;
  153.  
  154.     // Create a clown.
  155.     clown = new CClown("\pFred");
  156.  
  157.     // Add it to the database.
  158.     database->addObject(clown);
  159.  
  160.     // Build up its arsenal.
  161.     clown->bakePie("Jello");
  162.     clown->bakePie("Marshmellow");
  163.     clown->bakePie("Custard");
  164.     clown->bakePie("Cool Whip®");
  165.     clown->bakePie("Yogurt");
  166.  
  167.     // Remember to remove our reference when we're done.
  168.     clown->unrefer();
  169.     clown = nil;
  170.  
  171.     // Create another joker.
  172.     joker = new CJoker("\pHarry");
  173.  
  174.     // Add it to the database.
  175.     database->addObject(joker);
  176.  
  177.     // This guy steals jokes.
  178.     joker->learnJoke(joke2);
  179.  
  180.     // Remove our reference to the joker and the jokes.
  181.     joker->unrefer();
  182.     joker = nil;
  183.     joke1->unrefer();
  184.     joke1 = nil;
  185.     joke2->unrefer();
  186.     joke2 = nil;
  187. }
  188.  
  189. /***
  190.  * printOut
  191.  *
  192.  *    Find in turn each of the three objects in the database, two jokers and
  193.  *    a clown, then commit the changes to disk.
  194.  *
  195.  ***/
  196. void CLaughsDoc::printOut(void)
  197. {
  198.     CPerson *            person;
  199.     CNeoNameSelect        key("\p");
  200.     CNeoDatabase *        database        = getDatabase();
  201.     CNeoIndexIterator    iterator(database, kPersonID, &key, TRUE, TRUE);
  202.     CNeoString            string;
  203.     char                name[64];
  204.  
  205.     // Tell them what we're about to do.
  206.     database->getName(string);
  207.     BlockMove(&string[1], name, string[0]);
  208.     name[string[0]] = 0;
  209.     printf("Restoring %ld Jokers & %ld Clowns from \"%s\".\n",
  210.         database->getObjectCount(kJokerID, FALSE),
  211.         database->getObjectCount(kClownID, FALSE), name);
  212.  
  213.     // Let's make rand() a little more variable.
  214.     srand((int)time(nil));
  215.  
  216.     // Get the first person object.
  217.     person = (CPerson *)iterator.currentObject();
  218.  
  219.     while(person) {
  220.         person->printName();                        // Introductions.
  221.         person->skill();                            // Entertain the crowd.
  222.         printf("\n");
  223.         person = (CPerson *)iterator.nextObject();    // Next.
  224.     }
  225. }            
  226.  
  227.  
  228.